

#import "NSString+Extension.h"

@implementation NSString (Extension)

- (BOOL)isHasText {
    NSString *theString = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (theString.length >0) {
        return YES;
    }
    return NO;
}

+ (BOOL)isStringHasText:(NSString *)str
{
    NSString *theString = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (theString.length >0) {
        return YES;
    }
    return NO;
}

+ (BOOL)isEnoughLength:(NSString *)str maxLenght:(NSInteger)maxLength {
    NSString *string = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if(maxLength >= string.length > 0) {
        return YES;
    }
    return NO;
}


- (BOOL)isHaveTheString:(NSString *)theString {
    if([self rangeOfString:theString].location !=NSNotFound)//_roaldSearchText
    {
        return YES;
    } else {
        return NO;
    }
}

- (BOOL)stringToBool {
    if ([self isEqualToString:@"false"] || [self isEqualToString:@"FALSE"]) {
        return false;
    } else {
        return true;
    }
}

/**
 *  @author Hwang, 16-09-17 17:09:59
 *
 *  获取带有图标的富文本
 *
 *  @param image 图标图片
 *
 *  @return NSAttributedString
 */
//- (NSMutableAttributedString *)attribteWithImage:(UIImage *)image fontSize:(CGFloat)fontSize {
//    NSMutableAttributedString *text = [NSMutableAttributedString new];
//    UIFont *font = [UIFont systemFontOfSize:fontSize];
//    NSMutableAttributedString *attachment = nil;
//
//
//    attachment = [NSMutableAttributedString attachmentStringWithContent:image contentMode:UIViewContentModeTop attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentBottom];
//    [text appendAttributedString: attachment];
//    NSString *title = [NSString stringWithFormat:@"  %@", self];
//    [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
//    return text;
//}
+ (NSString *)lr_stringDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;
}

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}


- (NSString *)paramsStringWithParam:(NSDictionary *)params {
    if (params == nil) {
        return @"";
    }
    NSMutableString *postString = [NSMutableString string];
    NSArray *keys = [params allKeys];
    for (int i = 0; i < keys.count; i ++) {
        [postString appendFormat:@"%@=%@&", keys[i],params[keys[i]]];
    }
    return [NSString stringWithFormat:@"%@%@", self, [postString substringToIndex:([postString length]-1)]];
}

+ (NSString *)stringWithInt:(NSInteger)number {
    NSString *string;
    if (number > 9999) {
        string = [NSString stringWithFormat:@"%@.%@万", @(number / 10000),@(number % 10000 / 100)];
    } else {
        string = [NSString stringWithFormat:@"%ld", number];
    }
    return string;
}

- (NSString *)timeAgo{
    
    NSDateFormatter *date=[[NSDateFormatter alloc] init];
    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *oldDate =[date dateFromString:self];
    
    NSDate* nowDate = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSTimeInterval time=[nowDate timeIntervalSinceDate:oldDate];
    
    int days = ((int)time)/(3600*24);
    int hours = ((int)time)%(3600*24)/3600;
    int second = ((int)time)%(3600)/60;
    NSString *dateContent;
    if (hours <= 0 && second > 0) {
        dateContent = [[NSString alloc] initWithFormat:@"%i分前",second];
    } else if (days <= 0 && hours > 0) {
        dateContent = [[NSString alloc] initWithFormat:@"%i小时%i分前", hours, second];
    } else if(days > 0) {
        dateContent = [[NSString alloc] initWithFormat:@"%i天%i小时前",days,hours];
    } else if (days > 30) {
        dateContent = self;
    } else if (hours < 0 && second < 0) {
        dateContent = [[NSString alloc] initWithFormat:@"%i天%i小时后",abs(days),abs(hours)];
    }
    return dateContent;
}

- (NSString *)htmlStr {
    NSString *htmlString = [NSString stringWithFormat:@"<html> \n"
                            "<head> \n"
                            "<style type=\"text/css\"> \n"
                            "body {font-size:15px;}\n"
                            "</style> \n"
                            "</head> \n"
                            "<body>"
                            "<script type='text/javascript'>"
                            "window.onload = function(){\n"
                            "var $img = document.getElementsByTagName('img');\n"
                            "for(var p in  $img){\n"
                            " $img[p].style.width = '100%%';\n"
                            "$img[p].style.height ='auto'\n"
                            "}\n"
                            "}"
                            "</script>%@"
                            "</body>"
                            "</html>",self];
    return htmlString;
}

+ (NSString *)strWithFloat:(double)floatStr {
    return [NSString stringWithFormat:@"%g",floatStr];
}

+ (NSString *)strWithInteger:(NSInteger)integer {
    return [NSString stringWithFormat:@"%ld",integer];
}

@end
